﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EOS.DataModel;
using EOS.IBLL;
using EOS.Common;
using EOS.BLL;
using EOS.EntWebDemo.Models.Leave;
using System.Net;

namespace EOS.EntWebDemo.Controllers
{
    public class LeaveTestController : Controller
    {
        IUserInfoService _userInfoService = new UserInfoService();
        ILeaveService _leaveService = new LeaveService();

        private DataModelContainer db = new DataModelContainer();

        #region "flow"
        public ActionResult Test()
        {
            List<UserInfo> userInfos = _userInfoService.LoadEntities(u => u.ID != 0).ToList();
            return View(userInfos);
        }

        public ActionResult CreateLeaveStep()
        {
            return View();
        }

        public ActionResult EditLeaveStep(int? id)
        {
            //if (id == null)
            //{
            //    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            //}
            //UserInfo userInfo = db.UserInfo.Find(id);
            //if (userInfo == null)
            //{
            //    return HttpNotFound();
            //}
            return View();
        }

        [HttpPost]
        public ActionResult DeleteLeaveStep(int id)
        {
            UserInfo userInfo = db.UserInfo.Find(id);
            db.UserInfo.Remove(userInfo);
            db.SaveChanges();
            return RedirectToAction("Test");
        }
        #endregion

        // GET: LeaveTest
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Add()
        {
            LeaveView leaveView = new LeaveView();

            UserInfo uInfo = Session["UserInfo"] as UserInfo;
            //
            uInfo = new UserInfo();
            uInfo.ID = 3;
            uInfo.UName = "admin";
            //

            List<UserInfo> userInfos = _userInfoService.LoadEntities(u => u.ID != uInfo.ID).ToList();
            SelectList approverList = new SelectList(userInfos, "ID", "UName");
            ViewBag.approverList = approverList.AsEnumerable();


            ViewBag.leaveTypeList = new List<SelectListItem>() {
                new SelectListItem(){Value="1",Text="年假"},
                new SelectListItem(){Value="2",Text="病假"},
                new SelectListItem(){Value="3",Text="无薪假"}
            };

            leaveView.ApplicantID = uInfo.ID;
            leaveView.ApplicantName = uInfo.UName;

            leaveView.StartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 9, 0, 0);
            leaveView.EndTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 17, 0, 0);
            leaveView.LeaveTime = 8;
            leaveView.Status = string.Empty;
            leaveView.Reason = string.Empty;

            return View(leaveView);
        }
        [HttpPost]
        public ActionResult Add(LeaveView leaveView)
        {
            if (ModelState.IsValid == false)
            {

                ModelState.AddModelError("", "实体验证不合法");

                return View();
            }

            Leave leave = new Leave();
            leave.ApplicantID = leaveView.ApplicantID;
            leave.ApproverID = leaveView.ApproverID;
            leave.StartTime = leaveView.StartTime;
            leave.EndTime = leaveView.EndTime;
            leave.LeaveTime = leaveView.LeaveTime;
            leave.LeaveType = leaveView.LeaveType.ToString();
            leave.Reason = string.IsNullOrEmpty(leaveView.Reason) ? string.Empty : leaveView.Reason;
            leave.Status = string.IsNullOrEmpty(leaveView.Status) ? string.Empty : leaveView.Status;
            _leaveService.AddEntities(leave);
            return RedirectToAction("Index");
        }
    }
}